package com.example.homemanager.service;
import com.example.homemanager.model.SensorData;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.concurrent.atomic.AtomicReference;

@Service
public class SensorDataService {

    // 使用AtomicReference确保线程安全
    private final AtomicReference<SensorData> currentData = new AtomicReference<>(new SensorData());

    public SensorData getCurrentData() {
        SensorData data = currentData.get();
        data.setTimestamp(LocalDateTime.now());
        return data;
    }

    public void updateSensorData(SensorData newData) {
        // 更新时间戳
        newData.setTimestamp(LocalDateTime.now());
        currentData.set(newData);
    }

    public void setLightStatus(String status) {
        SensorData data = currentData.get();
        data.setLightStatus(status);
        data.setTimestamp(LocalDateTime.now());
        currentData.set(data);
    }
}